package com.hilin.base.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hilin.base.server.feign.api.IOrgApi;
import com.hilin.base.service.OrgService;
import com.hilin.common.common.BaseController;
import com.hilin.common.common.ReturnCode;
import com.hilin.common.util.AssertUtils;
import com.hilin.entity.po.AccountInfoPo;
import com.hilin.entity.po.OrgInfoPo;
import com.hilin.entity.vo.OrgInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@Api(tags = "机构")
@RestController
@RequestMapping("api/org")
public class OrgController implements IOrgApi {

    @Autowired
    private OrgService orgService;

    /**
     * 获取子机构列表
     * 只会查询下面一个层级
     *
     * @param orgId 机构ID，如果为空，则表示获取token所在机构信息
     * @return 机构列表
     */
    @ApiOperation("查询下一级机构列表")
    @GetMapping("/gen/childList")
    public List<OrgInfoPo> childList(@RequestHeader String token, @RequestParam(required = false) Long orgId) {
        return orgService.childList(Optional.ofNullable(orgId).orElse(orgId));
    }

    /**
     * 获取所有子机构树状列表
     * 树状结构
     *
     * @param orgId 机构ID，如果为空，则表示获取token所在机构信息
     * @return 机构列表
     */
    @GetMapping("/gen/childTreeList")
    public List<OrgInfoVo> childTreeList(@RequestHeader String token, @RequestParam(required = false) Long orgId) {
        return orgService.childTreeList(Optional.ofNullable(orgId).orElse(orgId));
    }

    /**
     * 查询子机构列表
     *
     * @param orgId 机构ID，如果为空，则表示获取token所在机构下属机构信息
     * @return 机构列表
     */
    @ApiOperation("查询子机构列表")
    @GetMapping("/gen/childAllList")
    public IPage<OrgInfoPo> childAllList(@RequestHeader String token, @RequestParam(required = false) Long orgId, String orgName, String orgAddress, Integer pagesize, Integer pagenum) {
        return orgService.childAllList(Optional.ofNullable(orgId).orElse(orgId), orgName, orgAddress, Optional.ofNullable(pagesize).orElse(10), Optional.ofNullable(pagenum).orElse(1));
    }

    /**
     * 获取机构信息
     *
     * @param orgId 如果为空，则表示获取token所在机构信息
     * @return 机构列表
     */
    @GetMapping("/gen/detail")
    public OrgInfoPo detail(@RequestHeader String token, @RequestParam(required = false) Long orgId) {
        orgId = Optional.ofNullable(orgId).orElse(BaseController.getSign().getOrgId());
        return orgService.detail(orgId);
    }

    /**
     * 创建机构
     *
     * @param orgInfo 机构信息，其中机构ID由系统生成，orgPid必须在当前token的下属机构中，如果orgPid为空，则表示在token所在机构下创建
     * @return 机构列表
     */
    @ApiOperation(value = "获取用户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "认证字符串", name = "token", required = true, paramType = "header"),
            @ApiImplicitParam(value = "机构信息", name = "orgInfo", required = true, dataTypeClass = OrgInfoPo.class),
    })
    @PostMapping("/admin/create")
    public OrgInfoPo create(@RequestHeader String token, OrgInfoPo orgInfo) {
        AssertUtils.isNotEmpty(orgInfo, ReturnCode.NORMAL_PARAM_LOST_ERROR.respMsg("参数不能为空"));
        AssertUtils.isNotEmpty(orgInfo.getOrgName(), ReturnCode.NORMAL_PARAM_LOST_ERROR.respMsg("机构名不能为空"));
        if (ObjectUtils.isEmpty(orgInfo.getOrgPid())) {
            orgInfo.setOrgPid(BaseController.getSign().getOrgId());
        }
        return orgService.create(orgInfo);
    }

    /**
     * 编辑机构
     *
     * @param orgId   机构id，为空则表示当前token所在机构
     * @param orgName 机构名
     * @return 机构列表
     */
    @PostMapping("/admin/edit")
    public void edit(@RequestHeader String token, @RequestParam(required = false) Long orgId, @RequestParam String orgName) {
        AssertUtils.isNotEmpty(orgId, ReturnCode.NORMAL_PARAM_LOST_ERROR.respMsg("机构ID不能为空"));
        AssertUtils.isNotEmpty(orgName, ReturnCode.NORMAL_PARAM_LOST_ERROR.respMsg("机构名不能为空"));
        if (ObjectUtils.isEmpty(orgId)) {
            orgId = BaseController.getSign().getOrgId();
        }
        orgService.edit(orgId, orgName);
    }

    /**
     * 编辑机构
     *
     * @param orgId 机构id，为空则表示当前token所在机构
     * @return 机构列表
     */
    @ApiOperation(value = "重置机构加密串")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "认证字符串", name = "token", required = true, paramType = "header"),
            @ApiImplicitParam(value = "机构ID，只允许当前token所在机构或下属机构，为空则表示当前token所在机构", name = "orgId"),
            @ApiImplicitParam(value = "已生成的token是否失效，默认否(false)", name = "invalidOldToken"),
    })
    @PostMapping("/admin/resetSecret")
    public String resetSecret(@RequestHeader String token, @RequestParam(required = false) Long orgId, @RequestParam(required = false) Boolean invalidOldToken) {
        AssertUtils.isNotEmpty(orgId, ReturnCode.NORMAL_PARAM_LOST_ERROR.respMsg("机构ID不能为空"));
        return orgService.resetSecret(orgId, Optional.ofNullable(invalidOldToken).orElse(false));
    }

    /**
     * 删除机构(软删除)
     *
     * @param orgId 机构ID，不允许删除当前token所在机构，只能删除下属机构
     */
    @ApiOperation(value = "获取某个机构下的用户")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "认证字符串", name = "token", required = true, paramType = "header"),
            @ApiImplicitParam(value = "机构ID，不允许当前token所在机构，只允许下属机构", name = "orgId"),
    })
    @PostMapping("/admin/delete")
    public void delete(@RequestHeader String token, @RequestParam Long orgId) {
        orgService.delete(orgId);
    }

    /**
     * 获取机构用户
     *
     * @param orgId 机构ID，只允许当前token所在机构或下属机构
     * @return 用户列表
     */
    @ApiOperation(value = "获取某个机构下的用户")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "认证字符串", name = "token", required = true, paramType = "header"),
            @ApiImplicitParam(value = "机构ID，只允许当前token所在机构或下属机构，为空则表示当前token所在机构", name = "orgId"),
    })
    @GetMapping("/admin/orgAccounts")
    public List<AccountInfoPo> orgAccounts(@RequestHeader String token, @RequestParam(required = false) Long orgId) {
        return null;
    }

    /**
     * 获取机构和下属机构用户
     *
     * @param orgId 机构ID，只允许当前token所在机构或下属机构
     * @return 用户列表
     */
    @ApiOperation(value = "获取机构和下属机构用户", notes = "该接口1分钟内只允许访问10次")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "认证字符串", name = "token", required = true, paramType = "header"),
            @ApiImplicitParam(value = "机构ID，只允许当前token所在机构或下属机构，为空则表示当前token所在机构", name = "orgId"),
    })
    @GetMapping("/admin/orgBranchAccounts")
    public List<AccountInfoPo> orgBranchAccounts(@RequestHeader String token, @RequestParam(required = false) Long orgId) {
        return null;
    }

}
